package cn.openea.perpartner.cms.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.cms.web.entity.SysRole;
import cn.openea.perpartner.cms.web.entity.SysUserRole;
import cn.openea.perpartner.cms.web.mapper.SysRoleMapper;
import cn.openea.perpartner.cms.web.mapper.SysUserRoleMapper;
import cn.openea.perpartner.cms.web.service.ISysRoleService;
import cn.openea.perpartner.common.core.constant.UserConstants;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Weird077
 * @description TODO
 * @date 2023-03-29 14:22
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Override
    public List<SysRole> selectRoleList(SysRole role) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        if (BeanUtil.isNotEmpty(role)) {
            return baseMapper.selectList(wrapper);
        }
        if (role.getRoleId() != null && role.getRoleId() != 0) {
            wrapper.eq(SysRole::getRoleId, role.getRoleId());
        }
        if (StrUtil.isNotEmpty(role.getRoleName())) {
            wrapper.eq(SysRole::getRoleName, role.getRoleName());
        }
        if (StrUtil.isNotEmpty(role.getStatus())) {
            wrapper.eq(SysRole::getStatus, role.getStatus());
        }
        if (StrUtil.isNotEmpty(role.getRoleKey())) {
            wrapper.eq(SysRole::getRoleKey, role.getRoleKey());
        }
        if (StrUtil.isNotEmpty(role.getDataScope())) {
            wrapper.eq(SysRole::getDataScope, role.getDataScope());
        }
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<SysRole> selectRolesByUserId(String userId) {
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getUserId, userId);
        List<SysUserRole> userRoles = userRoleMapper.selectList(wrapper);
        if (!userRoles.isEmpty()) {
            List<Long> userRolesList = new ArrayList<>();
            userRoles.forEach(userRole -> userRolesList.add(userRole.getRoleId()));
            LambdaQueryWrapper<SysRole> wrap = new LambdaQueryWrapper<>();
            wrap.in(SysRole::getRoleId, userRolesList);
            return baseMapper.selectList(wrap);
        }
        return Collections.emptyList();
    }

    @Override
    public String checkRoleNameUnique(SysRole role) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getRoleName, role.getRoleName());
        Long count = baseMapper.selectCount(wrapper);
        if (count >= 1) {
            return UserConstants.ROLE_NAME_NOT_UNIQUE;
        }
        return UserConstants.ROLE_NAME_UNIQUE;
    }

    @Override
    public String checkRoleKeyUnique(SysRole role) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getRoleKey, role.getRoleKey());
        Long count = baseMapper.selectCount(wrapper);
        if (count >= 1) {
            return UserConstants.ROLE_NAME_NOT_UNIQUE;
        }
        return UserConstants.ROLE_NAME_UNIQUE;
    }



}
